interface IAuthorization extends ng.IScope {
    AuthorizationChecked:AuthorizationChecked[];
    authorityList:{role:string[]};
    click():void;
    itemCheck(id:string,name:string):void;
    checkedAll():void;
    authorityType:string[];
}
interface AuthorizationChecked {
    id:string;
    level:string;
    name:string;
    newAuthorizationList:newAuthorizationList[];
}
interface newAuthorizationList {
    text:string;
    value:string;
    category:string;
}
function tmsAuthorization( valueService: IAuthorization,authorityService:authorityService): ng.IDirective{
    return {
        templateUrl:'./src/directives/tms-authorization/tms-authorization.html',
        restrict: 'E',
        transclude: true,
        replace: true,
        scope : {
            authorityList:"="
        },
        link  : ($scope: IAuthorization) =>{
            $scope.authorityList = {
                role:[]
            }
            authorityService.getList().then(res=>{
                $scope.AuthorizationChecked = res;
            });


            $scope.authorityType = [];
           
            //全选
            $scope.checkedAll = ()=>{
                $("#allCheck").is(":checked");
                if($("#allCheck").is(":checked")){
                    $scope.authorityList.role.length = 0;
                    $scope.authorityType.length = 0;
                    $scope.AuthorizationChecked.forEach((item,index)=>{
                        $scope.authorityType.push(item.id);
                        item.newAuthorizationList.forEach((items,indexs)=>{
                            $scope.authorityList.role.push(items.value);
        
                        });
                    });
                }else{
                    $scope.authorityList.role.length = 0;
                    $scope.authorityType.length = 0;
                }
            }


            //类别权限
            $scope.itemCheck = (id:string,name:string)=>{
                if($("#"+id).is(':checked')){
                    $scope.AuthorizationChecked.forEach((item,index)=>{
                    item.newAuthorizationList.forEach((items,indexs)=>{
                            if(name === items.category){
                                $scope.authorityList.role.forEach((itemC,indexC)=>{
                                    if(itemC===items.value){
                                        $scope.authorityList.role.splice(indexC,1)
                                    }
                                });
                                $scope.authorityList.role.push(items.value);
                            }
                        });
                    });
                }else{
                    $scope.AuthorizationChecked.forEach((item,index)=>{
                        item.newAuthorizationList.forEach((items,indexs)=>{
                            if(name === items.category){
                                $scope.authorityList.role.forEach((itemC,indexC)=>{
                                    if(itemC===items.value){
                                        $scope.authorityList.role.splice(indexC,1)
                                    }
                                });
                            }
                        });
                    });
                }
            };
            
            // $scope.click = ()=>{
            //     console.log($scope.authorityList.role)
            // }
        }
    };
}


tmsAuthorization['$inject'] = ["valueService","authorityService"]
angular.module('tmsAuthorization', []).directive('tmsAuthorization', tmsAuthorization);






